VB 如何判断数组为空 |
您所在的位置:网站首页 › vb6 数组 › VB 如何判断数组为空 |
方法一:Error方法 就是采用通用的错误捕获功能 On Error Goto 行号 '遇到错误,转到行号处处理 On Error Resume Next '忽略错误,继续执行 On Error Goto 0 '强制取消错误捕获功能 Function IsNotEmpty(ByVal sArray As Variant) As Boolean '判断数组是否为空 Dim i As Long IsNotEmpty = True On Error GoTo lerr: i = UBound(b) Exit Function lerr: IsNotEmpty = False End Function 方法二:CopyMemory方法 VB的数组都是安全数组,通过访问一个结构来确定 数组内容保存位置,上标下标和维数 安全数组结构的地址可以用 Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" _ (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) 安全数组的头两位就保存着维数信息 Option Explicit Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long) Private Sub Form_Load() Dim MyArr() As Long Dim pMyarr As Long Dim nDims As Integer '从数据指针得到SafeArray结构的指针 CopyMemory pMyarr, ByVal VarPtrArray(MyArr), 4 If pMyarr = 0 Then MsgBox "这个数组是空数组" '再从这个指针所指地址的头两个字节取出cDims CopyMemory nDims, ByVal pMyarr, 2 MsgBox "这个数组有" & nDims & "维" End If End Sub 方法三:使用api函数safearraygetdim()的返回值,返回值值 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |